#include "visual2.h"

visual2::visual2(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui_visual2)
{
    ui->setupUi(this);

    Paras::readConfig();
    
    this->setMinimumSize(800, 600);

    this->initUI();

    this->initButton();

    this->initEdit();

    this->initParas();

    init::getInstance()->setUI(ui);

    init::getInstance()->setParent(this);

    init::getInstance()->start();

    initChildThread::getInstance()->setUI(ui);

    initChildThread::getInstance()->setParent(this);

    initChildThread::getInstance()->initComponent();

    return;
}

visual2::~visual2()
{
    delete ui; 
}

void visual2::initUI()
{
    //初始化界面UI设置
    this->setWindowTitle("屏蔽罩方向检测工具");
    return;
}

void visual2::initButton()
{
    //初始化界面按钮设置
    ui->pushButton_model->setText("当前机型未选择");

    connect(ui->pushButton_connectPLC, SIGNAL(clicked()), this, SLOT(connectPLC()));
    connect(ui->pushButton_connectCamera, SIGNAL(clicked()), this, SLOT(connectCamera()));
    connect(ui->pushButton_model, SIGNAL(clicked()), this, SLOT(selectModel()));
    connect(ui->pushButton_exit, SIGNAL(clicked()), this, SLOT(exit()));
    connect(ui->pushButton_startCal, SIGNAL(clicked()), this, SLOT(startCalModel()));
    connect(ui->pushButton_reCal, SIGNAL(clicked()), this, SLOT(reCalModel()));
    connect(ui->pushButton_rotate, SIGNAL(clicked()), this, SLOT(rotate()));
    connect(ui->pushButton_calFinish, SIGNAL(clicked()), this, SLOT(calFinishModel()));
    connect(ui->pushButton_startCalCamera, SIGNAL(clicked()), this, SLOT(startCalCamera()));
    connect(ui->pushButton_reCalCamera, SIGNAL(clicked()), this, SLOT(reCalCamera()));
    connect(ui->pushButton_calFinishCamera, SIGNAL(clicked()), this, SLOT(calFinishCamera()));
    connect(ui->pushButton_x, SIGNAL(clicked()), this, SLOT(PLCTeachingX()));
    connect(ui->pushButton_y, SIGNAL(clicked()), this, SLOT(PLCTeachingY()));
    connect(ui->pushButton_z, SIGNAL(clicked()), this, SLOT(PLCTeachingZ()));
    connect(ui->pushButton_r, SIGNAL(clicked()), this, SLOT(PLCTeachingR()));
    return;
}

void visual2::initEdit()
{
    //令PLC地址输入框只能输入IP地址+端口格式
    ui->lineEdit_PLCAddress->setValidator(new QRegExpValidator(QRegExp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])\\b")));
    ui->lineEdit_PLCAddress->setInputMask("000.000.000.000:00000; ");
    ui->lineEdit_PLCAddress->setToolTip("请输入IP地址+端口号，如192.168.0.100:50000");

    //令相机曝光时间输入框只能输入数字
    ui->lineEdit_exposure->setValidator(new QIntValidator(0, 1000000, this));

    //令相机白平衡输入框只能输入类似于1.86|1.0|2.2的格式，并给出输入掩码和提示
    ui->lineEdit_wb->setValidator(new QRegExpValidator(QRegExp("\\b(\\d+\\.\\d+\\|){2}\\d+\\.\\d+\\b")));
    ui->lineEdit_wb->setInputMask("0.00|0.00|0.00; ");
    ui->lineEdit_wb->setToolTip("请输入白平衡参数，如1.86|1.0|2.2");

    //令图像处理参数输入框只能输入类似于255|80|25|51|1.3|0.8|1300000|500000的格式，并给出输入掩码和提示
    ui->lineEdit_process->setValidator(new QRegExpValidator(QRegExp("\\b(\\d+\\|){7}\\d+\\b")));
    ui->lineEdit_process->setInputMask("000|000|000|000|0.0|0.0|0000000|000000; ");
    ui->lineEdit_process->setToolTip("请输入图像处理参数，如255|80|25|51|1.3|0.8|1300000|500000");

    //令相机矫正矩阵输入框只能输入类似于1.0,0.0,0.0;0.0,1.0,0.0;0.0,0.0,1.0的格式，并给出输入掩码和提示
    ui->lineEdit_perspective->setValidator(new QRegExpValidator(QRegExp("\\b(\\d+\\.\\d+\\,){2}\\d+\\.\\d+\\;){2}(\\d+\\.\\d+\\,){2}\\d+\\.\\d+\\b")));
    ui->lineEdit_perspective->setInputMask("0.0,0.0,0.0|0.0,0.0,0.0|0.0,0.0,0.0; ");
    ui->lineEdit_perspective->setToolTip("请输入矫正矩阵，如1.0,0.0,0.0|0.0,1.0,0.0|0.0,0.0,1.0");

    //令左上角坐标输入框只能输入类似于1350,1350的格式，并给出输入掩码和提示
    ui->lineEdit_chessboardloc->setValidator(new QRegExpValidator(QRegExp("\\b\\d+\\,\\d+\\b")));
    ui->lineEdit_chessboardloc->setInputMask("0000,0000; ");
    ui->lineEdit_chessboardloc->setToolTip("请输入左上角坐标，如1350,1350");

    //令棋盘格角点数只能输入类似于9,6的格式，并给出输入掩码和提示
    ui->lineEdit_chessboardsize->setValidator(new QRegExpValidator(QRegExp("\\b\\d+\\,\\d+\\b")));
    ui->lineEdit_chessboardsize->setInputMask("0000,0000; ");
    ui->lineEdit_chessboardsize->setToolTip("请输入棋盘格角点数，如9,6");
    return;
}

/*void visual2::mouseEventFilter(bool b){

}*/

void visual2::initParas(){
    //获取当前时间
    Paras::time_start=time(NULL);
    ui->label_currentTime->setText(QString::fromStdString(ctime(&Paras::time_start)));
    ui->label_runningTime->setText("0");
    ui->label_productCount->setText("0");
    ui->label_capacity->setText("0");
    ui->label_status->setText("未运行");
    ui->listWidget_testing->clear();
    ui->listWidget_testing->addItem("软件已启动，当前时间：" + QString::fromStdString(ctime(&Paras::time_start)));
    ui->listWidget_testing->addItem("请连接相机与PLC，并选择机型");
    ui->tabWidget->setCurrentIndex(0);
    return;
}

// //初始化相机
// void visual2::initCamera(){
//     //使用opencv获取当前可用摄像头列表
//     cv::VideoCapture capture;
//     cv::Mat frame;
//     std::vector<cv::Point>cameraResolution;
//     int cameraNum = 0;
    
//     while (true)
//     {
//         capture.open(cameraNum);
//         if (!capture.isOpened())
//         {
//             break;
//         }
//         else
//         {
//             //获取当前相机的分辨率
//             capture >> frame;
//             cameraResolution.push_back(cv::Point(frame.cols, frame.rows));
//             cameraNum++;
//             capture.release();
//         }
//     }
//     //将可用摄像头列表添加到下拉菜单中
//     for (int i = 0; i < cameraNum; i++)
//     {
//         //获取第i个相机的名称
//         ui->comboBox_connectCamera->addItem(QString::number(i)+"号相机"+"("+QString::number(cameraResolution[i].x)+"x"+QString::number(cameraResolution[i].y)+")");
//     }
//     return;
// }

//连接至PLC
void visual2::connectPLC(){
    return;
}

//连接至相机
void visual2::connectCamera(){
    //获取当前选择的相机
    Paras::cameraIndex = ui->comboBox_connectCamera->currentIndex();
    //打开相机
    {
        QMutex
        if(Paras::cap.isOpened())
            Paras::cap.release();
        Paras::cap.open(Paras::cameraIndex, cv::CAP_DSHOW);
        if (!Paras::cap.isOpened())
        {
            QMessageBox::warning(this, "警告", "相机连接失败，请检查相机是否连接正确");
            return;
        }
    }
    //获取相机分辨率
    cv::Mat frame;
    Paras::cap >> frame;
    Paras::resolution = cv::Point(frame.cols, frame.rows);
    connect(Paras::timerCapture, SIGNAL(timeout()), this, SLOT(processImageAndUpgradeUI()));
    Paras::timerCapture->stop();
    Paras::timerCapture->start(Paras::timerCaptureInterval);
    Paras::is_cap_available=1;
    return;
}

void visual2::processImageAndUpgradeUI(){
    //获取当前时间
    Paras::time_now = time(NULL);
    //计算运行时间,单位为秒
    Paras::runningTime = (Paras::time_now - Paras::time_start);
    //计算产量
    Paras::capacity = Paras::cap_num / Paras::runningTime / 3600;
    //更新UI
    ui->label_currentTime->setText(QString::fromStdString(ctime(&Paras::time_now)));
    ui->label_runningTime->setText(QString::number(Paras::runningTime)+"s");
    ui->label_productCount->setText(QString::number(Paras::cap_num)+"个");
    ui->label_capacity->setText(QString::number(Paras::capacity)+"个/小时");
    ui->label_status->setText("运行中");
    return;
}

//选择机型
void visual2::selectModel(){
    return;
}

//退出
void visual2::exit(){
    //程序结束
    initChildThread::getInstance()->exitComponent();
    QApplication::exit();
    return;
}

//开始标定机型
void visual2::startCalModel(){
    if(Paras::is_cap_available==0){
        QMessageBox::warning(this, "警告", "请先连接相机");
        return;
    }
    Paras::changeMode(3);
    cv::Mat image;
    {
        QMutexLocker locker(&Paras::mutexImg);
        image=Paras::image.clone();
    }
    {
        QMutexLocker locker(&Paras::fixedframeMutexMat);
        Paras::fixedframe=image.clone();
    }
    return;
}


//重新标定机型
void visual2::reCalModel(){
    {
        QMutexLocker locker(&Paras::mutexMode);
        Paras::Work_state=2;
    }
    return;
}

//旋转
void visual2::rotate(){
    return;
}

//标定完成机型
void visual2::calFinishModel(){
    //若当前未框选，则提示并返回
    int width,height;
    cv::Point2d center;
    {
        QMutexLocker locker(&Paras::mutexTemp);
        width=Paras::tempwidth;
        height=Paras::tempheight;
        center=Paras::tempCenter;
    }
        if(width==0||height==0){
        QMessageBox::warning(this, "警告", "请先框选");
        return;
    }
    //截取当前框选的图片
    cv::Mat image;
    {
        QMutexLocker locker(&Paras::fixedframeMutexMat);
        image=Paras::fixedframe.clone();
    }
    cv::Mat imageROI=image(cv::Rect(center.x-width/2,center.y-height/2,width,height));
    //将截取的图片作为匹配模板保存，保存路径为Paras::tempPath+Paras::modelName
    std::string path=(Paras::tempPath+"/"+Paras::modelName+".png").toStdString();
    qDebug()<<QString::fromStdString(path);
    qDebug()<<imageROI.cols<<imageROI.rows;
    bool res=cv::imwrite(path,imageROI);
    qDebug()<<res;
    //将当前框选的图片作为模板显示
    {
        QMutexLocker locker(&Paras::fixedframeMutexMat);
        Paras::fixedframe=imageROI.clone();
    }
    return;
}

//开始标定相机
void visual2::startCalCamera(){
    return;
}

//重新标定相机
void visual2::reCalCamera(){
    return;
}

//标定完成相机
void visual2::calFinishCamera(){
    return;
}

//操作PLCx方向
void visual2::PLCTeachingX(){
    return;
}

//操作PLCy方向
void visual2::PLCTeachingY(){
    return;
}

//操作PLCz方向
void visual2::PLCTeachingZ(){
    return;
}

//操作PLCr方向
void visual2::PLCTeachingR(){
    return;
}

void visual2::mousePressEvent(QMouseEvent *event){
    cv::Point2d mousePos,imagePos,imgPosInLabel;
    if(event->button() == Qt::LeftButton){
        //获取鼠标位置
        mousePos = cv::Point2d(event->pos().x(), event->pos().y());
        //获取图像在label中的位置
        imgPosInLabel = cv::Point2d((ui->label_display->width() - Paras::resolution.x)/2, (ui->label_display->height() - Paras::resolution.y)/2);
        //将鼠标位置转换为图像坐标
        imagePos = cv::Point2d(mousePos.x - ui->label_display->x() - imgPosInLabel.x, mousePos.y - ui->label_display->y() - imgPosInLabel.y);
        qDebug()<<"PRESS"<<imagePos.x<<imagePos.y;
        //若鼠标位置不在图像范围内，则返回
        if(imagePos.x < 0 || imagePos.y < 0 || imagePos.x > Paras::resolution.x || imagePos.y > Paras::resolution.y)
            return;
        //如果当前左上角点和右上角点位置都是(0,0)，则将鼠标位置作为左上角点并重新开始画图
        if(Paras::tempLeftTop.x == 0 && Paras::tempLeftTop.y == 0 && Paras::tempRightTop.x == 0 && Paras::tempRightTop.y == 0){
            Paras::tempLeftTop = imagePos;
            Paras::currentPoint=2;
            return;
        }
        //判断鼠标位置离哪个点最近
        double minDistance = 1000000;
        int minIndex = 0;
        std::vector<cv::Point2d> tempPoints;
        //将四个点加入到tempPoints中
        {
            QMutexLocker locker(&Paras::mutexTemp);
            tempPoints.push_back(Paras::tempLeftTop);
            tempPoints.push_back(Paras::tempRightTop);
            tempPoints.push_back(Paras::tempRightButtom);
            tempPoints.push_back(Paras::tempLeftButtom);
        }
        for(int i = 0; i < 4; i++){
            double distance = sqrt(pow(imagePos.x - tempPoints[i].x, 2) + pow(imagePos.y - tempPoints[i].y, 2));
            if(distance < minDistance){
                minDistance = distance;
                minIndex = i;
            }
        }
        //若鼠标位置离四个点都很远，则判断鼠标位置是否在四个点围成的矩形内
        //鼠标位置远近判断为鼠标位置离四个点最小距离大于四个点围成矩形长与宽最小值的五分之一
        int minSide = std::min(abs(tempPoints[1].x - tempPoints[0].x), abs(tempPoints[2].y - tempPoints[0].y));
        if(minDistance > minSide / 5){
            //若鼠标位置不在四个点围成的矩形内，则当前点为左上角点，重新绘制矩形
            if(imagePos.x < tempPoints[0].x || imagePos.x > tempPoints[1].x || imagePos.y < tempPoints[0].y || imagePos.y > tempPoints[2].y){
                QMutexLocker locker(&Paras::mutexTemp);
                Paras::tempLeftTop= imagePos;
                Paras::currentPoint = 2;
            }
            //若鼠标位置在四个点围成的矩形内，将当前点赋值给矩形中心
            else{
                QMutexLocker locker(&Paras::mutexTemp);
                Paras::tempCenter = imagePos;
                Paras::currentPoint = 4;
            }
            QMutexLocker locker(&Paras::mutexTemp);
            Paras::tempLeftTop= imagePos;
            Paras::currentPoint = 2;
        }
        //否则找到最近的点，将当前点赋值给最近的点
        else{
            QMutexLocker locker(&Paras::mutexTemp);
            switch(minIndex){
            case 0:
                Paras::tempLeftTop = imagePos;
                Paras::currentPoint = 0;
                break;
            case 1:
                Paras::tempRightTop = imagePos;
                Paras::currentPoint = 1;
                break;
            case 2:
                Paras::tempRightButtom = imagePos;
                Paras::currentPoint = 2;
                break;
            case 3:
                Paras::tempLeftButtom = imagePos;
                Paras::currentPoint = 3;
                break;
            }
        }
    }
    return;
}

void visual2::mouseMoveEvent(QMouseEvent *event){
    cv::Point2d mousePos,imagePos,imgPosInLabel;
    //获取鼠标位置
    mousePos = cv::Point2d(event->pos().x(), event->pos().y());
    //获取图像在label中的位置
    imgPosInLabel = cv::Point2d((ui->label_display->width() - Paras::resolution.x)/2, (ui->label_display->height() - Paras::resolution.y)/2);
    //将鼠标位置转换为图像坐标
    imagePos = cv::Point2d(mousePos.x - ui->label_display->x() - imgPosInLabel.x, mousePos.y - ui->label_display->y() - imgPosInLabel.y);
    if(imagePos.x < 0 || imagePos.y < 0 || imagePos.x > Paras::resolution.x || imagePos.y > Paras::resolution.y)
        return;
    //根据当前点移动矩形，固定其对角点，另外两点随当前点一起变化
    double minX,minY,maxX,maxY;
    {
        QMutexLocker locker(&Paras::mutexTemp);
        qDebug()<<Paras::currentPoint;
        switch(Paras::currentPoint){
            case 0:
                Paras::tempLeftTop = imagePos;
                minX = std::min(Paras::tempLeftTop.x,Paras::tempRightButtom.x);
                minY = std::min(Paras::tempLeftTop.y,Paras::tempRightButtom.y);
                maxX = std::max(Paras::tempLeftTop.x,Paras::tempRightButtom.x);
                maxY = std::max(Paras::tempLeftTop.y,Paras::tempRightButtom.y);
                break;
            case 1:
                Paras::tempRightTop = imagePos;
                minX = std::min(Paras::tempRightTop.x,Paras::tempLeftButtom.x);
                minY = std::min(Paras::tempRightTop.y,Paras::tempLeftButtom.y);
                maxX = std::max(Paras::tempRightTop.x,Paras::tempLeftButtom.x);
                maxY = std::max(Paras::tempRightTop.y,Paras::tempLeftButtom.y);
                break;
            case 2:
                Paras::tempRightButtom = imagePos;
                minX = std::min(Paras::tempRightButtom.x,Paras::tempLeftTop.x);
                minY = std::min(Paras::tempRightButtom.y,Paras::tempLeftTop.y);
                maxX = std::max(Paras::tempRightButtom.x,Paras::tempLeftTop.x);
                maxY = std::max(Paras::tempRightButtom.y,Paras::tempLeftTop.y);
                break;
            case 3:
                Paras::tempLeftButtom = imagePos;
                minX = std::min(Paras::tempLeftButtom.x,Paras::tempRightTop.x);
                minY = std::min(Paras::tempLeftButtom.y,Paras::tempRightTop.y);
                maxX = std::max(Paras::tempLeftButtom.x,Paras::tempRightTop.x);
                maxY = std::max(Paras::tempLeftButtom.y,Paras::tempRightTop.y);
                break;
            case 4:
                double dx = imagePos.x - Paras::tempCenter.x;
                double dy = imagePos.y - Paras::tempCenter.y;
                //判断移动后左上点是否在图像范围内
                if(Paras::tempLeftTop.x + dx >= 0 || Paras::tempLeftTop.x + dx <= Paras::resolution.x || Paras::tempLeftTop.y + dy >= 0 || Paras::tempLeftTop.y + dy <= Paras::resolution.y)
                    Paras::tempLeftTop = cv::Point2d(Paras::tempLeftTop.x + dx, Paras::tempLeftTop.y + dy);
                //判断移动后右上点是否在图像范围内
                if(Paras::tempRightTop.x + dx >= 0 || Paras::tempRightTop.x + dx <= Paras::resolution.x || Paras::tempRightTop.y + dy >= 0 || Paras::tempRightTop.y + dy <= Paras::resolution.y)
                    Paras::tempRightTop = cv::Point2d(Paras::tempRightTop.x + dx, Paras::tempRightTop.y + dy);
                //判断移动后右下点是否在图像范围内
                if(Paras::tempRightButtom.x + dx >= 0 || Paras::tempRightButtom.x + dx <= Paras::resolution.x || Paras::tempRightButtom.y + dy >= 0 || Paras::tempRightButtom.y + dy <= Paras::resolution.y)
                    Paras::tempRightButtom = cv::Point2d(Paras::tempRightButtom.x + dx, Paras::tempRightButtom.y + dy);
                //判断移动后左下点是否在图像范围内
                if(Paras::tempLeftButtom.x + dx >= 0 || Paras::tempLeftButtom.x + dx <= Paras::resolution.x || Paras::tempLeftButtom.y + dy >= 0 || Paras::tempLeftButtom.y + dy <= Paras::resolution.y)
                    Paras::tempLeftButtom = cv::Point2d(Paras::tempLeftButtom.x + dx, Paras::tempLeftButtom.y + dy);
                Paras::tempCenter = imagePos;
                break;
        }
        if(Paras::currentPoint != 4){
            Paras::tempwidth= maxX - minX;
            Paras::tempheight = maxY - minY;
            Paras::tempCenter = cv::Point2d((minX + maxX)/2,(minY + maxY)/2);
            Paras::tempLeftTop = cv::Point2d(minX,minY);
            Paras::tempRightTop = cv::Point2d(maxX,minY);
            Paras::tempRightButtom = cv::Point2d(maxX,maxY);
            Paras::tempLeftButtom = cv::Point2d(minX,maxY);
            if(imagePos==Paras::tempLeftTop)
                Paras::currentPoint=0;
            else if(imagePos==Paras::tempRightTop)
                Paras::currentPoint=1;
            else if(imagePos==Paras::tempRightButtom)
                Paras::currentPoint=2;
            else if(imagePos==Paras::tempLeftButtom)
                Paras::currentPoint=3;
        }
    }
    return;
}

void visual2::mouseReleaseEvent(QMouseEvent *event){
    cv::Point2d mousePos,imagePos,imgPosInLabel;
    if(event->button() == Qt::LeftButton){
        //获取鼠标位置
        mousePos = cv::Point2d(event->pos().x(), event->pos().y());
        //获取图像在label中的位置
        imgPosInLabel = cv::Point2d((ui->label_display->width() - Paras::resolution.x)/2, (ui->label_display->height() - Paras::resolution.y)/2);
        //将鼠标位置转换为图像坐标
        imagePos = cv::Point2d(mousePos.x - ui->label_display->x() - imgPosInLabel.x, mousePos.y - ui->label_display->y() - imgPosInLabel.y);
        qDebug()<<"RELEASE"<<imagePos.x<<imagePos.y;
        //若鼠标位置不在图像范围内，则修正为图像边界
        if(imagePos.x < 0 || imagePos.y < 0 || imagePos.x > Paras::resolution.x || imagePos.y > Paras::resolution.y){
            if(imagePos.x < 0)
                imagePos.x = 0;
            if(imagePos.y < 0)
                imagePos.y = 0;
            if(imagePos.x > Paras::resolution.x)
                imagePos.x = Paras::resolution.x;
            if(imagePos.y > Paras::resolution.y)
                imagePos.y = Paras::resolution.y;
        }
        //找到当前选择点的对角点，并以这两个点构建矩形
        double minX,minY,maxX,maxY;
        {
            QMutexLocker locker(&Paras::mutexTemp);
            switch(Paras::currentPoint){
                case 0:
                    Paras::tempLeftTop = imagePos;
                    minX = std::min(Paras::tempLeftTop.x,Paras::tempRightButtom.x);
                    minY = std::min(Paras::tempLeftTop.y,Paras::tempRightButtom.y);
                    maxX = std::max(Paras::tempLeftTop.x,Paras::tempRightButtom.x);
                    maxY = std::max(Paras::tempLeftTop.y,Paras::tempRightButtom.y);
                    break;
                case 1:
                    Paras::tempRightTop = imagePos;
                    minX = std::min(Paras::tempRightTop.x,Paras::tempLeftButtom.x);
                    minY = std::min(Paras::tempRightTop.y,Paras::tempLeftButtom.y);
                    maxX = std::max(Paras::tempRightTop.x,Paras::tempLeftButtom.x);
                    maxY = std::max(Paras::tempRightTop.y,Paras::tempLeftButtom.y);
                    break;
                case 2:
                    Paras::tempRightButtom = imagePos;
                    minX = std::min(Paras::tempRightButtom.x,Paras::tempLeftTop.x);
                    minY = std::min(Paras::tempRightButtom.y,Paras::tempLeftTop.y);
                    maxX = std::max(Paras::tempRightButtom.x,Paras::tempLeftTop.x);
                    maxY = std::max(Paras::tempRightButtom.y,Paras::tempLeftTop.y);
                    break;
                case 3:
                    Paras::tempLeftButtom = imagePos;
                    minX = std::min(Paras::tempLeftButtom.x,Paras::tempRightTop.x);
                    minY = std::min(Paras::tempLeftButtom.y,Paras::tempRightTop.y);
                    maxX = std::max(Paras::tempLeftButtom.x,Paras::tempRightTop.x);
                    maxY = std::max(Paras::tempLeftButtom.y,Paras::tempRightTop.y);
                    break;
            }
            if(Paras::currentPoint == 4){
                double dx = imagePos.x - Paras::tempCenter.x;
                double dy = imagePos.y - Paras::tempCenter.y;
                //判断移动后左上点是否在图像范围内
                if(Paras::tempLeftTop.x + dx >= 0 || Paras::tempLeftTop.x + dx <= Paras::resolution.x || Paras::tempLeftTop.y + dy >= 0 || Paras::tempLeftTop.y + dy <= Paras::resolution.y)
                    Paras::tempLeftTop = cv::Point2d(Paras::tempLeftTop.x + dx, Paras::tempLeftTop.y + dy);
                //判断移动后右上点是否在图像范围内
                if(Paras::tempRightTop.x + dx >= 0 || Paras::tempRightTop.x + dx <= Paras::resolution.x || Paras::tempRightTop.y + dy >= 0 || Paras::tempRightTop.y + dy <= Paras::resolution.y)
                    Paras::tempRightTop = cv::Point2d(Paras::tempRightTop.x + dx, Paras::tempRightTop.y + dy);
                //判断移动后右下点是否在图像范围内
                if(Paras::tempRightButtom.x + dx >= 0 || Paras::tempRightButtom.x + dx <= Paras::resolution.x || Paras::tempRightButtom.y + dy >= 0 || Paras::tempRightButtom.y + dy <= Paras::resolution.y)
                    Paras::tempRightButtom = cv::Point2d(Paras::tempRightButtom.x + dx, Paras::tempRightButtom.y + dy);
                //判断移动后左下点是否在图像范围内
                if(Paras::tempLeftButtom.x + dx >= 0 || Paras::tempLeftButtom.x + dx <= Paras::resolution.x || Paras::tempLeftButtom.y + dy >= 0 || Paras::tempLeftButtom.y + dy <= Paras::resolution.y)
                    Paras::tempLeftButtom = cv::Point2d(Paras::tempLeftButtom.x + dx, Paras::tempLeftButtom.y + dy);
                Paras::tempCenter = imagePos;
            }
            Paras::tempwidth= maxX - minX;
            Paras::tempheight = maxY - minY;
            Paras::tempCenter = cv::Point2d((minX + maxX)/2,(minY + maxY)/2);
            Paras::tempLeftTop = cv::Point2d(minX,minY);
            Paras::tempRightTop = cv::Point2d(maxX,minY);
            Paras::tempRightButtom = cv::Point2d(maxX,maxY);
            Paras::tempLeftButtom = cv::Point2d(minX,maxY);
        }
    }
    return;
}
